package com.withmiku.world.user.common;

import lombok.Data;

@Data
public class Result<T> {
    private Integer code;   // 状态码
    private String message; // 返回信息
    private T data; // 返回数据内容

    // 成功 - 有数据
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("success");
        result.setData(data);
        return result;
    }

    // 成功 - 无数据
    public static <T> Result<T> success() {
        return success(null);
    }

    // 失败 - 有数据
    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    // 自定义失败状态码
    public static <T> Result<T> error(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    // 自定义成功状态码
    public static <T> Result<T> success(int code, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }
}
